home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-23 | 12.2 KB | 279 lines | [TEXT/ttxt] |
- 8 Batches and Programs
- MacDOS recognises four types of "executable" files:
- • Applications: Files of type 'APPL'.
- • Compiled AppleScripts: Files of type 'osas'.
- • Text AppleScripts, of type 'TEXT' and creator 'ToyS'.
- • Batch programs, of type 'TEXT' containing MacDOS
- commands to be interpreted and executed one line at a
- time.
-
- You have two ways of telling MacDOS what program to
- execute:
- • by writing its file specification at the beginning
- of a command line;
- • by executing the command call fileSpec .
- The two strategies are only different if you are already
- executing in batch: in the first case, control is simply
- transferred to the new program; with CALL, execution of the
- current batch resumes after the application has been
- launched or the new batch program has terminated.
-
- Instead of having to type full file specifications, you can
- take advantage of the system variable PATH and just type
- the name of the application or batch program. If you keep
- all applications and batch programs (or their aliases) in a
- small number of folders, you will never have to type a full
- file specification at all.
-
- Note that MacDOS, unlike DOS, does not automatically add
- the extension .EXE when looking for executable files.
- Neverthess, similarly to DOS, MacDOS automatically adds the
- extension .BAT when looking for batch programs.
-
- Introduction to Batch Programs
- After opening a batch file for reading, MacDOS reads and
- attempts to execute one line of text at a time. In its
- simplest form, a batch program is therefore just a way of
- storing a series of MacDOS commands so that you do not need
- to type them over and over again.
-
- Nevertheless, there are a series of features which make
- batch programs very useful:
- • Several MacDOS commands are only available within
- batch programs.
- • You can control the execution of batch programs
- through launch parameters.
- • You can setup a batch program in such a way that it
- behaves like a Macintosh application.
- • You can document your algorithms and the reasoning
- behind them by adding lines of comments.
-
- To add lines of comments, you can use the command REM (for
- REMark). REM tells MacDOS to ignore the rest of the line
- and continue with the next one. You can also use an
- exclamation mark ('!') in the same way.
-
- If you redirect the output of a batch program, MacDOS
- automatically applies the redirection to each individual
- command in the batch file, including nested batches. Note
- that the output of batch programs cannot be piped.
-
- Jumps and Labels
- Normally, MacDOS executes the lines of a batch program in
- sequence, as they appear in the file. You can direct MacDOS
- to interrupt the sequence and resume execution somewhere
- else in the file by using the command GOTO label.
-
- Almost any word or number can be used as a label but, to be
- recognised and accepted as such, it must satisfy the
- following conditions:
- • It cannot contain spaces or tabs.
- • It cannot begin with a percent sign.
- • It must be the first word in a line which begins
- with a colon.
- • It must be unique within the file.
-
- Before executing a batch program, MacDOS scans the whole
- file looking for lines which begin with a colon. For each
- one of such lines, MacDOS builds a "label entry" containing
- the first word after the colon and the file offset of the
- next line. Note that MacDOS ignores whatever follows the
- label, so that you can use the rest of the line for
- commenting. Also note that the colon identifies the label
- but is not part of it.
-
- When MacDOS encounters a GOTO during batch execution, it
- scans the label entries looking for the requested label. If
- it finds an entry with the matching label, it then resumes
- execution of the batch program from the line with the file
- offset found in the entry.
-
- Replaceable Parameters
- Whether you just type a batch filename or use the command
- CALL, you can pass to the batch program a series of
- parameters.
-
- The parameter %0 stores the name of the batch file itself,
- while the parameters %1 to %9 are available for you to
- define as you need.
-
- Each one of these replaceable parameters consists of a
- string and is accessible from within the batch program
- through a number preceded by a percent sign. For example,
- if you type:
- myBatch name "a full address"
- the program myBatch will be able to access the string
- name as %1 and the string a full address as %2 . The
- string myBatch will be available as %0 .
-
- Despite the limitation in the number of parameters, MacDOS
- lets you pass to a batch program up to 24 strings. You can
- access the additional 15 strings by using the command
- SHIFT. SHIFT copies %1 to %0, %2 to %1, ..., and %9 to %8;
- it then stores the 10th string into %9. By "pushing in" one
- new string at a time in this way, you can acess all 15
- additional strings.
-
- Conditional Statements
- MacDOS lets you implement conditional statements with the
- command IF. The general format of IF is if condition do
- aCommand , where aCommand is executed only if condition
- is true.
-
- Valid conditions are:
- string1 == string2
- exist fileSpec
- existdir folderSpec
- not existdir folderSpec
-
- As you can see from the last example, you can also invert a
- condition by inserting a 'not' between the IF and the
- condition.
-
- Loops
- Very often, a particular sequence of commands needs to be
- repeated for each file belonging to a particular set or
- until certain special conditions are verified. To serve
- this purpose, MacDOS provides the command: FOR.
-
- FOR lets you define a set of files and then operate on each
- one of them. In its simplest form, FOR executes a single
- command included in the same line:
- for %name in (set) do aCommand
- where set is a comma-separated list of file specifications,
- and aCommand can refer to the current file through the
- special variable %name.
-
- To execute several commands for each file, you can use an
- extended format of FOR:
- for %name in (set) do begin
- ... several commands which can refer to the current
- file through the
- global variable %name% ...
- next name
-
- The most general way of implementing loops is with a
- combination of an IF and a GOTO. For example, if you need
- to repeat certain operations until a certain condition is
- verified, you can do as follows (equivalent a WHILE or a
- FOR in C or a WHILE-DO in Pascal):
- set k = 1
- :LOOP_LBL
- if k == 5 goto BREAK_LBL
- ... here you do what you need to do ...
- incr k
- goto LOOP_LBL
- :BREAK_LBL
-
- Alternatively, you can place the IF at the end of the loop
- (equivalent to a DO in C or Fortran or a REPEAT-UNTIL in
- Pascal):
- set control=initial
- :LOOP_LBL
- ... here you do what you need to do ...
- ... and also set the control variable in order to
- exit the loop ...
- if control==done goto BREAK_LBL
- goto LOOP_LBL
- :BREAK_LBL
-
- Error Handling
- Beside reporting error messages in clear, MacDOS also
- stores error codes into the global variable doserr. To
- convert the error codes to their corresponding messages,
- you can use the command SHOW.
-
- The command ONERROR lets you trap all the errors which
- occur while executing a batch program. This is particularly
- useful when you execute a batch program with ECHO OFF,
- because in that case MacDOS suppresses error and warning
- messages.
-
- ONERROR lets you specify the label of an error handling
- procedure. For example,
- onerror label
- ... first command for which you want to trap errors ...
- ... second command for which you want to trap errors
- ...
- ...
- onerror
- is functionally equivalent to the following less readable
- construct:
- set doserr=0
- ... first command for which you want to trap errors ...
- if not %doserr% == 0 goto label
- ... second command for which you want to trap errors
- ...
- if not %doserr% == 0 goto label
- ...
- set doserr=0
-
- User Interface
- While executing in batch, MacDOS normally displays all
- commands as they are executed. With the command ECHO OFF,
- you can direct MacDOS to suppress both the echoing of the
- commands and their output, including warning and error
- messages. You can then re-enable the display with ECHO ON.
-
- If you only want to suppress the output of a single
- command, you can do so by attaching an '@' to the front of
- the command name.
-
- By typing as first line of a batch program the command
- @echo off , you make the program completely silent.
-
- In order to display variable values and other messages
- while echoing is disabled, you can use ECHO as illustrated
- in the following examples:
- echo Now processing file No %fileNum%
- echo File %1 found: it is in folder %foldSpec%
-
- After ECHOing an intermediate result, you might need to
- suspend execution of a batch program and wait for
- confirmation before continuing. For this purpose you can
- use the command PAUSE. When MacDOS encounters PAUSE, it
- displays the message:
- press any key to continue ...
- and waits for your reply. If you type cmd-dot or cntl-C,
- MacDOS then asks
- Terminate batch job (Y/N) ?
- You can then terminate the job by replying 'Y' or 'y'.
-
- Batches that behave like Applications
- Any file of type 'TEXT' is accepted by MacDOS as a valid
- batch program. Nevertheless, if you want to automatically
- launch MacDOS by double clicking on the icon of a batch
- file, you need to set its creator to 'mDOS'.
-
- By using the command EXIT as last line, you can also
- automatically terminate MacDOS and return to the Finder at
- the end of the batch program.
-
- Here is a simple program that is actually used every day to
- back up this User's Guide while it is being written:
- confirm off
- copy "\MacDOS folder\product documents\manual" 2: /u/v
- exit
- It copies to a floppy all files which have been updated
- since the last backup.
-
- You can preset to 'mDOS' the creator of text files by
- setting the system variable CREATOR:
- set creator=mDOS
-
- Alternatively, you can change to 'mDOS' the creator of
- existing files:
- ren/c!mDOS fileSpec
-
- Note that MacDOS executes the batch program autoexec.bat
- (see below) before executing the batch program that you
- have double clicked.
-
- If you keep MacDOS running all the times, instead of
- aliasing the MacDOS application into the folder "Startup
- Items", you might like to replace all the items in that
- folder with a small batch program which CALLs them all.
- They would be launched in the order in which you call them
- (MacDOS will always be the first one, though).
-
-